import com.gitee.chessboard.jdbc.JdbcManager;
import com.gitee.chessboard.jdbc.executor.SqlExecutor;
import com.gitee.chessboard.jdbc.parser.annotations.Id;
import com.gitee.chessboard.model.dictionary.Dictionary;
import com.gitee.chessboard.model.word.Word;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.List;

/**
 * 测试能否将常量解析成字典
 *
 * @author 敲代码的旺财
 * @date 2021/11/24 16:32
 */
public class DictionaryGetAllTest {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(DictionaryGetAllTest.class.getClassLoader().getResourceAsStream("mybatis.xml"));
        Configuration configuration = sqlSessionFactory.getConfiguration();
        PooledDataSource dataSource = (PooledDataSource) configuration.getEnvironment().getDataSource();
        JdbcManager jdbcManager = new JdbcManager(
                dataSource.getDriver()
                , dataSource.getUrl()
                , dataSource.getUsername()
                , dataSource.getPassword());
        jdbcManager.setMybatisSqlSessionFactory(sqlSessionFactory);

        SqlExecutor sqlExecutor = jdbcManager.getSqlExecutor();
        String dictionaryClazz = "com.gitee.chessboard.model.dictionary.DefaultDictionary";
        String wordClazz = "com.gitee.chessboard.model.word.DefaultWord";
        Dictionary dictionary = (Dictionary) Class.forName(dictionaryClazz).newInstance();
        Word word = (Word) Class.forName(wordClazz).newInstance();

        //读取数据

        //获取所有的字典
        List<Dictionary> dictionaryAll = sqlExecutor.getAll(dictionary);
        //获取所有的词
        List<Word> wordAll = sqlExecutor.getAll(word);
        System.out.println(wordAll);
    }

}
